package com.lyl.syt.api;

import com.lyl.syt.common.result.Result;
import com.lyl.syt.model.hosp.Schedule;
import com.lyl.syt.modules.mongodb.service.ScheduleService;
import com.lyl.syt.vo.hosp.ScheduleOrderVo;
import com.lyl.syt.vo.hosp.ScheduleQueryVo;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController("ApiScheduleController")
@RequestMapping("/api/hosp")
@RequiredArgsConstructor
public class ScheduleController extends BaseAPI{
    private final ScheduleService scheduleService;

    @ApiOperation("根据排班id获取预约下单数据")
    @GetMapping("/inner/getScheduleOrderVo/{scheduleId}")
    public ScheduleOrderVo getScheduleOrderVo(@PathVariable String scheduleId){
        return scheduleService.getScheduleOrderVo(scheduleId);
    }


    @ApiOperation("根据排班id获取排班数据")
    @GetMapping("/getSchedule/{scheduleId}")
    public Result<Schedule> getScheduleById(@PathVariable String scheduleId){
        Schedule schedule = scheduleService.getScheduleById(scheduleId);
        return Result.ok(schedule);
    }


    @ApiOperation("获取可以预约的排班数据")
    @GetMapping("/auth/getBookingScheduleRule/{page}/{limit}/{hoscode}/{depcode}")
    public Result<Map<String,Object>> getBookingSchedule(@PathVariable Integer page,
                                     @PathVariable Integer limit,
                                     @PathVariable String hoscode,
                                     @PathVariable String depcode){
        return Result.ok(scheduleService.getBookingScheduleRules(page,limit,hoscode,depcode));
    }

    /**
     * 删除排班信息
     * @param request 请求参数
     * @return boolean
     */
    @PostMapping("/schedule/remove")
    public Result<Boolean> removeSchedule(HttpServletRequest request){
        //进行前置处理，进行签名校验
        Map<String, Object> paramMap = prefixHandler(request);

        //医院编号
        String hoscode = (String) paramMap.get("hoscode");
        String hosScheduleId = (String) paramMap.get("hosScheduleId");
        //删除指定排班信息
        scheduleService.remove(hoscode,hosScheduleId);
        return Result.ok();
    }

    /**
     * 查询排班信息
     * @param request 请求参数
     * @return 分页数据
     */
    @PostMapping("/schedule/list")
    public Result<Page<Schedule>> findScheduleList(HttpServletRequest request){
        //进行前置处理，进行签名校验
        Map<String, Object> paramMap = prefixHandler(request);

        //医院编号
        String hoscode = (String) paramMap.get("hoscode");
        String depcode = (String) paramMap.get("depcode");
        //分页参数
        int page = Integer.parseInt((String) paramMap.getOrDefault("page", 1));
        int limit = Integer.parseInt((String) paramMap.getOrDefault("limit", 5));

        ScheduleQueryVo queryVo = new ScheduleQueryVo();
        queryVo.setHoscode(hoscode);
        queryVo.setDepcode(depcode);

        //查询数据
        return Result.ok(scheduleService.findPageSchedule(page,limit,queryVo));

    }

    /**
     * 上传排班接口
     * @param request
     * @return
     */
    @PostMapping("/saveSchedule")
    public Result<Boolean> saveSchedule(HttpServletRequest request){
        //进行前置处理，进行签名校验
        Map<String, Object> paramMap = prefixHandler(request);
        //上传排班信息
        scheduleService.save(paramMap);
        return Result.ok();
    }
}
